package com.xjoyt.pay.gateway.controllers;

import java.io.BufferedReader;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xjoyt.pay.dto.AccessParam;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.gateway.annotations.CallbackApi;
import com.xjoyt.pay.gateway.annotations.RestApi;
import com.xjoyt.pay.gateway.constants.RestApiConstant;
import com.xjoyt.pay.gateway.params.CallbackParam;
import com.xjoyt.pay.gateway.params.RefundParam;
import com.xjoyt.pay.master.dto.CallbackResult;
import com.xjoyt.pay.master.dto.RefundResult;
import com.xjoyt.pay.master.interfaces.RefundContract;

@RestController
@RequestMapping("refund")
public class RefundController extends GatewayAbstractController
        implements com.xjoyt.pay.gateway.interfaces.RefundContract {

    @Autowired
    private RefundContract contract;

    @RestApi(value = RestApiConstant.ORDER_BANK_PAY, name = "退款")
    @Override
    public ExecuteResult<RefundResult> create(
            @RequestBody(required = false) AccessParam<RefundParam> param) {
        param.param.merchantId = getMerchant(param.accessToken).id;
        param.param.callbackUrl = getCallbackUrl();
        return contract.create(param.param);
    }

    /**
     * 统一回调处理
     * 
     * @param param
     * @return
     */
    @CallbackApi("退款统一回调")
    @RequestMapping("callback/{id}")
    public String callback(@PathVariable String id, Model model) {
        HttpServletRequest request = getHttpServletRequest();
        StringBuilder body = new StringBuilder();
        try (BufferedReader br = request.getReader()) {
            String str;
            while ((str = br.readLine()) != null) {
                body.append(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        CallbackParam param = new CallbackParam();
        param.id = id;
        param.body = body.toString();
        param.params = request.getParameterMap();
        try {
            ExecuteResult<CallbackResult> result = contract.callback(param);
            model.addAttribute("callback", result.getValue());
            model.addAttribute("param", param);

            return result.getValue().callbackResponse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String getCallbackUrl() {
        if (isProduct())
            return "https://api.xjockj.com/open/v1/refund/callback/";
        if (isDeveloper())
            return getRequestPath() + "/refund/callback/";
        return "https://api.open.xjoyt.com/open/v1/refund/callback/";
    }

}
